//
//  MeVC.m
//  YangWifi
//
//  Created by zuoxueliangduyuanzhuo on 2017/6/10.
//  Copyright © 2017年 zuoxueliangduyuanzhuo. All rights reserved.
//

#import "MeVC.h"
#import "MeCell.h"
#import "MeHeaderV.h"
#import "MeFooterV.h"
#import "LoginVC.h"
#import "MeProtocolVC.h"
#import "MeAboutVC.h"
#import "MeFeedbackVC.h"
#import <UShareUI/UShareUI.h>
#import "ActionSheetView.h"
#import "MeInfoVC.h"
#import "UserInfoModel.h"

@interface MeVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic ,strong) MeHeaderV *headV;

@property (nonatomic ,strong) MeFooterV *footV;

@end

@implementation MeVC

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    
    [self.headV buildData];
    [self.footV buildData];
    [self.table reloadData];
    
    if ([AccountHanler isLogin]) {
        [self getUserInfo];

    }
    
    
}

- (void)getUserInfo
{
    NSDictionary *params = @{
                             @"type": @"OSWIFI_USERINFO_MOBILE",
                             @"token":IsNull([AccountHanler token], @""),
                             @"requestEntity": @{
                                     @"telephone":IsNull([AccountHanler telPhone], @""),
                                        @"method":@"query",
                                     }
                             };
    @WeakObj(self);
    [MHNetworkManager postReqeustWithURL:nil params:params successBlock:^(NSDictionary *returnData) {
        XLBaseModel *result = [XLBaseModel mj_objectWithKeyValues:returnData];
        if (result.resultFlag) {
            
            UserInfoModel *model = [UserInfoModel mj_objectWithKeyValues:result.responseEntity[0]];
            [AccountHanler saveUserInfo:model];
            [Weakself.headV buildData];

        }else{
            DLog(@"用户信息获取失败");
        }
    } failureBlock:^(NSError *error) {
        DLog(@"用户信息获取失败");
    } showHUD:NO];
}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self.data addObject:@{@"title":@"关于我们"}];
    
    [self.data addObject:@{@"title":@"用户协议"}];
   
    [self.data addObject:@{@"title":@"建议反馈"}];
    
    [self.data addObject:@{@"title":@"商务合作"}];
    
    [self.data addObject:@{@"title":@"分享"}];
    
    self.table.tableHeaderView = self.headV;
    self.table.tableFooterView = self.footV;
    
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    if (@available(iOS 11.0, *)) {
        
        self.table.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        
    } else {
        
        self.automaticallyAdjustsScrollViewInsets = NO;
        
    }
    
    self.table.bounces = NO;
    [self.table registerNib:[UINib nibWithNibName:@"MeCell" bundle:nil] forCellReuseIdentifier:@"MeCell"];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(exitLoginHandler) name:@"exitLoginHandler" object:nil];

    
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"exitLoginHandler" object:nil];
}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.data.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 56;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"MeCell";
    MeCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    cell.data = self.data[indexPath.row];
    cell.lineV.hidden = (indexPath.row == self.data.count-1)?YES:NO;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.row == 0){
        
        MeAboutVC *vc = [[MeAboutVC alloc] init];
        [self showViewController:vc sender:nil];

    }else if (indexPath.row == 1) {
        
        MeProtocolVC *vc = [[MeProtocolVC alloc] init];
        [self showViewController:vc sender:nil];
    
    }else if (indexPath.row == 2) {
        
        if (![AccountHanler isLogin]) {
            
            LoginVC *loginVC = [[LoginVC alloc] initWithNibName:@"LoginVC" bundle:nil];
            loginVC.isNeedOrderNoti = YES;
            loginVC.isNeedHomeNoti = YES;
            @WeakObj(self);
            loginVC.loginHandler = ^{
                MeFeedbackVC *vc = [[MeFeedbackVC alloc] init];
                [Weakself showViewController:vc sender:nil];
            };
            XLNavigationVC *nav = [[XLNavigationVC alloc] initWithRootViewController:loginVC];
            [self presentViewController:nav animated:YES completion:^{
                
            }];
        }else{
            MeFeedbackVC *vc = [[MeFeedbackVC alloc] init];
            [self showViewController:vc sender:nil];
        }
        

        
    }else if (indexPath.row == 3){
        NSMutableString* str=[[NSMutableString alloc] initWithFormat:@"telprompt://%@",@"021-64807818"];
        // NSLog(@"str======%@",str);
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str] options:@{} completionHandler:^(BOOL success) {
            
        }];
    }else if (indexPath.row == 4){
        NSArray *titlearr = @[@"朋友圈",@"微信",@"QQ"];
        
        NSArray *imageArr = @[@"umsocial_wechat_timeline",@"umsocial_wechat",@"umsocial_qq"];
        
        ActionSheetView *actionsheet = [[ActionSheetView alloc] initWithShareHeadOprationWith:titlearr andImageArry:imageArr andProTitle:@"测试" and:ShowTypeIsShareStyle];
        __weak typeof(self) weakSelf = self;
        [actionsheet setBtnClick:^(NSInteger btnTag) {
            UMSocialPlatformType type = UMSocialPlatformType_WechatTimeLine;
            // 这里面可以加入分享到某个第三方的点击
            if (btnTag == 0) {
                type = UMSocialPlatformType_WechatTimeLine;
            }else if (btnTag == 1){
                type = UMSocialPlatformType_WechatSession;
            }else if (btnTag == 2){
                type = UMSocialPlatformType_QQ;
            }
            [weakSelf shareDataWithPlatform:type];
            
        }];
        [[UIApplication sharedApplication].keyWindow addSubview:actionsheet];
    }
    
}

//创建分享内容对象
- (UMSocialMessageObject *)creatMessageObject
{
    //创建分享消息对象
    UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
    NSString *title = @"洋葱侠";
    NSString *url =@"http://www.baidu.com";
    NSString *text = @"洋葱侠";
    
    UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:title descr:text thumImage:[UIImage imageNamed:@"lanuch_icon"]];
    [shareObject setWebpageUrl:url];
    messageObject.shareObject = shareObject;
    
    return messageObject;
}

//直接分享
- (void)shareDataWithPlatform:(UMSocialPlatformType)platformType
{
    
    UMSocialMessageObject *messageObject = [self creatMessageObject];
    //调用分享接口
    
    [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
        
        NSString *message = nil;
        if (!error) {
            message = [NSString stringWithFormat:@"分享成功"];
        }
        else{
            if (error) {
                message = [NSString stringWithFormat:@"失败原因Code: %d\n",(int)error.code];
            }
            else{
                message = [NSString stringWithFormat:@"分享失败"];
            }
        }
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"洋葱侠"
                                                        message:message
                                                       delegate:nil
                                              cancelButtonTitle:NSLocalizedString(@"确定", nil)
                                              otherButtonTitles:nil];
        [alert show];
    }];
}



#pragma mark-
#pragma mark action

- (void)loginHandler
{
    DLog(@"123");
    
    LoginVC *loginVC = [[LoginVC alloc] initWithNibName:@"LoginVC" bundle:nil];
    loginVC.isNeedOrderNoti = YES;
    loginVC.isNeedHomeNoti = YES;
    @WeakObj(self);
    loginVC.loginHandler = ^{
        
        [Weakself.table reloadData];
    };
    XLNavigationVC *nav = [[XLNavigationVC alloc] initWithRootViewController:loginVC];
    [self presentViewController:nav animated:YES completion:^{
        
    }];
}

- (void)exitLoginHandler
{
    [self.footV buildData];
    [self.headV buildData];
    [self.table reloadData];
}

- (void)changeAccountHandler
{
    DLog(@"456");
    [AccountHanler removeIsLogin];
    [self.footV buildData];
    [self.headV buildData];
    [self.table reloadData];
}

- (void)pushMeInfo
{
    MeInfoVC *vc = [[MeInfoVC alloc] init];
    [self showViewController:vc sender:nil];
}

#pragma mark-
#pragma mark set&&get
- (MeHeaderV *)headV
{
    if (!_headV) {
        _headV = [MeHeaderV instanceMeHeaderView];
        _headV.frame = CGRectMake(0, 0, SCREEN_WIDTH, 100);
        _headV.autoresizingMask = UIViewAutoresizingNone;
        @WeakObj(self);
        _headV.headerHandler = ^{

            if (![AccountHanler isLogin]) {
                [Weakself loginHandler];
            }else{
                [Weakself pushMeInfo];
            }
        };
    }
    return _headV;
}

- (MeFooterV *)footV
{
    if (!_footV) {
        _footV = [MeFooterV instanceMeFooterView];
        _footV.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-49-100-56*5);
        _footV.autoresizingMask = UIViewAutoresizingNone;

        @WeakObj(self);
        _footV.footerHandler = ^{
            
            if ([AccountHanler isLogin]) {
                [Weakself changeAccountHandler];
            }
        };
    }
    return _footV;
}

@end
